{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 15.4 \u5728C\u6269\u5c55\u6a21\u5757\u4e2d\u64cd\u4f5c\u9690\u5f62\u6307\u9488\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u6709\u4e00\u4e2a\u6269\u5c55\u6a21\u5757\u9700\u8981\u5904\u7406C\u7ed3\u6784\u4f53\u4e2d\u7684\u6307\u9488\uff0c\n\u4f46\u662f\u4f60\u53c8\u4e0d\u60f3\u66b4\u9732\u7ed3\u6784\u4f53\u4e2d\u4efb\u4f55\u5185\u90e8\u7ec6\u8282\u7ed9Python\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u9690\u5f62\u7ed3\u6784\u4f53\u53ef\u4ee5\u5f88\u5bb9\u6613\u7684\u901a\u8fc7\u5c06\u5b83\u4eec\u5305\u88c5\u5728\u80f6\u56ca\u5bf9\u8c61\u4e2d\u6765\u5904\u7406\u3002\n\u8003\u8651\u6211\u4eec\u4f8b\u5b50\u4ee3\u7801\u4e2d\u7684\u4e0b\u5217C\u4ee3\u7801\u7247\u6bb5\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "typedef struct Point {\n    double x,y;\n} Point;\n\nextern double distance(Point *p1, Point *p2);"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528\u80f6\u56ca\u5305\u88c5Point\u7ed3\u6784\u4f53\u548c distance() \u51fd\u6570\u7684\u6269\u5c55\u4ee3\u7801\u5b9e\u4f8b\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "/* Destructor function for points */\nstatic void del_Point(PyObject *obj) {\n  free(PyCapsule_GetPointer(obj,\"Point\"));\n}\n\n/* Utility functions */\nstatic Point *PyPoint_AsPoint(PyObject *obj) {\n  return (Point *) PyCapsule_GetPointer(obj, \"Point\");\n}\n\nstatic PyObject *PyPoint_FromPoint(Point *p, int must_free) {\n  return PyCapsule_New(p, \"Point\", must_free ? del_Point : NULL);\n}\n\n/* Create a new Point object */\nstatic PyObject *py_Point(PyObject *self, PyObject *args) {\n\n  Point *p;\n  double x,y;\n  if (!PyArg_ParseTuple(args,\"dd\",&x,&y)) {\n    return NULL;\n  }\n  p = (Point *) malloc(sizeof(Point));\n  p->x = x;\n  p->y = y;\n  return PyPoint_FromPoint(p, 1);\n}\n\nstatic PyObject *py_distance(PyObject *self, PyObject *args) {\n  Point *p1, *p2;\n  PyObject *py_p1, *py_p2;\n  double result;\n\n  if (!PyArg_ParseTuple(args,\"OO\",&py_p1, &py_p2)) {\n    return NULL;\n  }\n  if (!(p1 = PyPoint_AsPoint(py_p1))) {\n    return NULL;\n  }\n  if (!(p2 = PyPoint_AsPoint(py_p2))) {\n    return NULL;\n  }\n  result = distance(p1,p2);\n  return Py_BuildValue(\"d\", result);\n}"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728Python\u4e2d\u53ef\u4ee5\u50cf\u4e0b\u9762\u8fd9\u6837\u6765\u4f7f\u7528\u8fd9\u4e9b\u51fd\u6570\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import sample\np1 = sample.Point(2,3)\np2 = sample.Point(4,5)\np1"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "p2"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "sample.distance(p1,p2)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u80f6\u56ca\u548cC\u6307\u9488\u7c7b\u4f3c\u3002\u5728\u5185\u90e8\uff0c\u5b83\u4eec\u83b7\u53d6\u4e00\u4e2a\u901a\u7528\u6307\u9488\u548c\u4e00\u4e2a\u540d\u79f0\uff0c\u53ef\u4ee5\u4f7f\u7528 PyCapsule_New() \u51fd\u6570\u5f88\u5bb9\u6613\u7684\u88ab\u521b\u5efa\u3002\n\u53e6\u5916\uff0c\u4e00\u4e2a\u53ef\u9009\u7684\u6790\u6784\u51fd\u6570\u80fd\u88ab\u7ed1\u5b9a\u5230\u80f6\u56ca\u4e0a\uff0c\u7528\u6765\u5728\u80f6\u56ca\u5bf9\u8c61\u88ab\u5783\u573e\u56de\u6536\u65f6\u91ca\u653e\u5e95\u5c42\u7684\u5185\u5b58\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8981\u63d0\u53d6\u80f6\u56ca\u4e2d\u7684\u6307\u9488\uff0c\u53ef\u4f7f\u7528 PyCapsule_GetPointer() \u51fd\u6570\u5e76\u6307\u5b9a\u540d\u79f0\u3002\n\u5982\u679c\u63d0\u4f9b\u7684\u540d\u79f0\u548c\u80f6\u56ca\u4e0d\u5339\u914d\u6216\u5176\u4ed6\u9519\u8bef\u51fa\u73b0\uff0c\u90a3\u4e48\u5c31\u4f1a\u629b\u51fa\u5f02\u5e38\u5e76\u8fd4\u56deNULL\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u672c\u8282\u4e2d\uff0c\u4e00\u5bf9\u5de5\u5177\u51fd\u6570\u2014\u2014 PyPoint_FromPoint() \u548c PyPoint_AsPoint()\n\u88ab\u7528\u6765\u521b\u5efa\u548c\u4ece\u80f6\u56ca\u5bf9\u8c61\u4e2d\u63d0\u53d6Point\u5b9e\u4f8b\u3002\n\u5728\u4efb\u4f55\u6269\u5c55\u51fd\u6570\u4e2d\uff0c\u6211\u4eec\u4f1a\u4f7f\u7528\u8fd9\u4e9b\u51fd\u6570\u800c\u4e0d\u662f\u76f4\u63a5\u4f7f\u7528\u80f6\u56ca\u5bf9\u8c61\u3002\n\u8fd9\u79cd\u8bbe\u8ba1\u4f7f\u5f97\u6211\u4eec\u53ef\u4ee5\u5f88\u5bb9\u6613\u7684\u5e94\u5bf9\u5c06\u6765\u5bf9Point\u5e95\u4e0b\u7684\u5305\u88c5\u7684\u66f4\u6539\u3002\n\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u51b3\u5b9a\u4f7f\u7528\u53e6\u5916\u4e00\u4e2a\u80f6\u56ca\u4e86\uff0c\u90a3\u4e48\u53ea\u9700\u8981\u66f4\u6539\u8fd9\u4e24\u4e2a\u51fd\u6570\u5373\u53ef\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5bf9\u4e8e\u80f6\u56ca\u5bf9\u8c61\u4e00\u4e2a\u96be\u70b9\u5728\u4e8e\u5783\u573e\u56de\u6536\u548c\u5185\u5b58\u7ba1\u7406\u3002\nPyPoint_FromPoint() \u51fd\u6570\u63a5\u53d7\u4e00\u4e2a must_free \u53c2\u6570\uff0c\n\u7528\u6765\u6307\u5b9a\u5f53\u80f6\u56ca\u88ab\u9500\u6bc1\u65f6\u5e95\u5c42Point * \u7ed3\u6784\u4f53\u662f\u5426\u5e94\u8be5\u88ab\u56de\u6536\u3002\n\u5728\u67d0\u4e9bC\u4ee3\u7801\u4e2d\uff0c\u5f52\u5c5e\u95ee\u9898\u901a\u5e38\u5f88\u96be\u88ab\u5904\u7406\uff08\u6bd4\u5982\u4e00\u4e2aPoint\u7ed3\u6784\u4f53\u88ab\u5d4c\u5165\u5230\u4e00\u4e2a\u88ab\u5355\u72ec\u7ba1\u7406\u7684\u5927\u7ed3\u6784\u4f53\u4e2d\uff09\u3002\n\u7a0b\u5e8f\u5458\u53ef\u4ee5\u4f7f\u7528 extra \u53c2\u6570\u6765\u63a7\u5236\uff0c\u800c\u4e0d\u662f\u5355\u65b9\u9762\u7684\u51b3\u5b9a\u5783\u573e\u56de\u6536\u3002\n\u8981\u6ce8\u610f\u7684\u662f\u548c\u73b0\u6709\u80f6\u56ca\u6709\u5173\u7684\u6790\u6784\u5668\u80fd\u4f7f\u7528 PyCapsule_SetDestructor() \u51fd\u6570\u6765\u66f4\u6539\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5bf9\u4e8e\u6d89\u53ca\u5230\u7ed3\u6784\u4f53\u7684C\u4ee3\u7801\u800c\u8a00\uff0c\u4f7f\u7528\u80f6\u56ca\u662f\u4e00\u4e2a\u6bd4\u8f83\u5408\u7406\u7684\u89e3\u51b3\u65b9\u6848\u3002\n\u4f8b\u5982\uff0c\u6709\u65f6\u5019\u4f60\u5e76\u4e0d\u5173\u5fc3\u66b4\u9732\u7ed3\u6784\u4f53\u7684\u5185\u90e8\u4fe1\u606f\u6216\u8005\u5c06\u5176\u8f6c\u6362\u6210\u4e00\u4e2a\u5b8c\u6574\u7684\u6269\u5c55\u7c7b\u578b\u3002\n\u901a\u8fc7\u4f7f\u7528\u80f6\u56ca\uff0c\u4f60\u53ef\u4ee5\u5728\u5b83\u4e0a\u9762\u653e\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684\u5305\u88c5\u5668\uff0c\u7136\u540e\u5c06\u5b83\u4f20\u7ed9\u5176\u4ed6\u7684\u6269\u5c55\u51fd\u6570\u3002"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}